import 'package:flutter/material.dart';

///数字变化监听，increment代表是否增加
typedef NumberChange(bool increment);

class PlusMinus extends StatelessWidget {
  int count;
  final NumberChange numberChange;

  PlusMinus({count, this.numberChange}) : super() {
    try {
      this.count = int.parse('$count');
    } catch (e) {
      this.count = 1;
    }
  }

  void _minus() {
    if (count > 1) {
      if (numberChange != null) numberChange(false);
    }
  }

  void _plus() {
    if (numberChange != null) numberChange(true);
  }

  @override
  Widget build(BuildContext context) => Align(
        alignment: Alignment.centerLeft,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            GestureDetector(
              child: Image.asset(
                count == null || count <= 1
                    ? 'imgs/js_cells_img_amount_minus_disable.png'
                    : 'imgs/js_cells_img_amount_minus.png',
                width: 30.0,
                height: 30.0,
              ),
              onTap: _minus,
            ),
            Container(
              child: Center(
                child: Text(
                  '${count <= 1 ? 1 : count}',
                  maxLines: 1,
                  style: TextStyle(
                    fontSize: 18.0,
                  ),
                ),
              ),
              width: 50.0,
            ),
            GestureDetector(
              child: Image.asset(
                'imgs/js_cells_img_amount_plus.png',
                width: 30.0,
                height: 30.0,
              ),
              onTap: _plus,
            ),
          ],
        ),
      );
}
